package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.core.helper.DictHelper;
import com.bifang.module.system.core.dao.model.SysUser;
import com.bifang.module.system.core.dao.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.enums.EduWorkOrderLogTypeDictEnum;
import com.jwsoft.manager.common.vo.eduWorkOrderLog.EduWorkOrderLogKeyVO;
import com.jwsoft.manager.common.vo.eduWorkOrderLog.EduWorkOrderLogQueryVO;
import com.jwsoft.manager.common.vo.eduWorkOrderLog.EduWorkOrderLogVO;
import com.jwsoft.manager.common.vo.eduWorkOrderLog.LogTimeLineVO;
import com.jwsoft.manager.core.dao.model.EduWorkOrderLog;
import com.jwsoft.manager.core.dao.service.EduWorkOrderLogService;
import com.jwsoft.manager.core.integration.EduWorkOrderLogIntegration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 工单表业务实现类
 *
 * @author zhiyuan
 * @since 2023-02-28
 */
@ApiService(funcCode = "workOrder", title = "工单表")
public class EduWorkOrderLogIntegrationImpl implements EduWorkOrderLogIntegration {

    @Autowired
    public EduWorkOrderLogService eduWorkOrderLogService;


    @Autowired
    public DictHelper dictHelper;

    @Autowired
    public SysUserService sysUserService;


    @Override
    @OpApi(funcCode = "workOrderLog0001", title = "工单表分页查询", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE)
    public PageInfo<EduWorkOrderLogVO> getList(EduWorkOrderLogQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<EduWorkOrderLog> list = eduWorkOrderLogService.list();
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduWorkOrderLog> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduWorkOrderLogVO> resultList = Convert.toList(EduWorkOrderLogVO.class, pageInfo.getList());
        // 转义数据字典值
//         dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "workOrderLog0002", title = "工单表根据主键查询详情", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE)
    public EduWorkOrderLogVO getById(EduWorkOrderLogKeyVO vo) {
        EduWorkOrderLog entity = eduWorkOrderLogService.getById(vo.getWorkOrderLogId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduWorkOrderLogVO result = Convert.convert(EduWorkOrderLogVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "workOrderLog0003", title = "工单表保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduWorkOrderLogVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "workOrderLog0004", title = "工单表新增", funcType = FuncTypeEnum.insert,checkPrivilege = BoolEnum.FALSE)
    @SaveOplog(
            operationType = "workOrder0004",
            operationName = "工单表新增",
            dataType = "workOrderLogId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduWorkOrderLogVO add(EduWorkOrderLogVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        EduWorkOrderLog entity = Convert.convert(EduWorkOrderLog.class, vo);
        eduWorkOrderLogService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setWorkOrderLogId(entity.getWorkOrderLogId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "workOrderLog0005", title = "工单表修改", funcType = FuncTypeEnum.update,checkPrivilege = BoolEnum.FALSE)
    @SaveOplog(
            operationType = "workOrder0005",
            operationName = "工单表修改",
            dataType = "workOrderLogId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduWorkOrderLogVO edit(EduWorkOrderLogVO vo) {
        if (ObjectUtils.isEmpty(vo.getWorkOrderLogId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduWorkOrderLog oldEntity = eduWorkOrderLogService.getById(vo.getWorkOrderLogId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduWorkOrderLog entity = Convert.convert(EduWorkOrderLog.class, vo);
        eduWorkOrderLogService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "workOrderLog0006", title = "工单表根据主键删除", funcType = FuncTypeEnum.delete,checkPrivilege = BoolEnum.FALSE)
    @SaveOplog(
            operationType = "workOrder0006",
            operationName = "工单表删除",
            dataType = "workOrderLogId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduWorkOrderLogKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getWorkOrderLogId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduWorkOrderLog entity = eduWorkOrderLogService.getById(vo.getWorkOrderLogId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduWorkOrderLogService.removeById(vo.getWorkOrderLogId());
    }

    @OpApi(funcCode = "workOrderLog0007", title = "查询工单表处理时间线", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE)
    @Override
    public List<LogTimeLineVO> queryByOrderWork(EduWorkOrderLogKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getWorkOrderId())){
            throw new AppException("传入的工单id不能为空!");
        }
        QueryWrapper<EduWorkOrderLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EduWorkOrderLog::getWorkOrderId,vo.getWorkOrderId()).orderByAsc(EduWorkOrderLog::getHandleTime);
        List<EduWorkOrderLog> list = eduWorkOrderLogService.list(queryWrapper);
        List<LogTimeLineVO> data = new ArrayList<>();
        for (EduWorkOrderLog eduWorkOrderLog : list) {
            LogTimeLineVO log = new LogTimeLineVO();
            log.setFullName(eduWorkOrderLog.getFullName());
            log.setHandleTime(eduWorkOrderLog.getHandleTime());
            log.setType(eduWorkOrderLog.getType());
            if (eduWorkOrderLog.getHandleTime() != null){
                log.setHandleMessage(eduWorkOrderLog.getHandleMessage());
            }
            data.add(log);

            if (eduWorkOrderLog.getNextUserId() != null  ){
                SysUser user = sysUserService.getById(eduWorkOrderLog.getNextUserId());
                LogTimeLineVO logTimeLineVO = new LogTimeLineVO();
                if (!ObjectUtils.isEmpty(user) && !ObjectUtils.isEmpty(user.getFullName())){
                    logTimeLineVO.setFullName(user.getFullName());
                }
                logTimeLineVO.setHandleTime(eduWorkOrderLog.getHandleTime());
                data.add(logTimeLineVO);
                if (eduWorkOrderLog.getHandleTime() != null){
                    logTimeLineVO.setHandleMessage(eduWorkOrderLog.getHandleMessage());
                }
            }
        }


         dictHelper.valueToName(data, Arrays.asList(EduWorkOrderLogTypeDictEnum.values()));
        return data;
    }
}
